<?php

HBase::using('DAL.HPages');

class HApplication {
	function __construct() {		
		
	}
	
	/**
	 * Runnable
	 *
	 */
	public function run() {		
		// Prepare the request
		self::prepare();
		
		// Resolve Request
		self::resolveRequest();
		
		// Authenticate User / Role / ACL	
		HAuthentication::check();
		
		// Render 		
		self::renderPage();
	}
	
	public function renderPage() {
		$page = $this->hPage;		
		// Get layout by page alias
		$layout = HPages::getLayoutByPageAlias($page);		
		
		// Require the layout file
				require_once(SYSTEM_PATH.SEPARATOR.'layouts'.SEPARATOR.$layout.SEPARATOR.'layout.tpl');
	}
	
	public function resolveRequest() {		
		if (!isset($this->hGet['p'])) {
			$this->hPage = 'Home';
		}
		else {
			$this->hPage = HCommon::xss($this->hGet['p']);
			if (!self::validatePage()) $this->hPage = 'Home';				
		}
	}
	
	public function validatePage() {
		$a = HSql::fetch2Array(HSql::doQuery("SELECT page_alias FROM portal_pages WHERE page_alias = '".$this->hPage."'"));
		if (count($a) == 0)
			return false;		
		else 			
			return true;		
	}	
	
	public function prepare() {
		//if(get_magic_quotes_gpc())
		{
			if(isset($_GET))
				$this->hGet=$this->stripSlashes($_GET);
			if(isset($_POST))
				$this->hPost=$this->stripSlashes($_POST);
			if(isset($_REQUEST))
				$this->hRequest=$this->stripSlashes($_REQUEST);
			if(isset($_COOKIE))
				$this->hCookie=$this->stripSlashes($_COOKIE);
		}
	}

	/**
	 * Strips slashes from input data.
	 * This method is applied when magic quotes is enabled.
	 * @param mixed input data to be processed
	 * @return mixed processed data
	 */
	public function stripSlashes(&$data)
	{
		return is_array($data)?array_map(array($this,'stripSlashes'),$data):stripslashes($data);
	}
	
	public function getCurrentAlias() {		
		return $this->hPage;
	}
	
	
	public $hPageHolder;
	/**		 
	 * $_page
	 * @var string
	 */
	public $hPage;
	
	/**		 
	 * $_POST alias
	 * @var array
	 */
	public $hPost;
	
	/**		 
	 * $_GET alias
	 * @var array
	 */
	public $hGet;
	
	/**		 
	 * $_REQUEST alias
	 * @var array
	 */
	public $hRequest;
	
	/**		 
	 * $_COOKIE alias
	 * @var array
	 */
	public $hCookie;
}